/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility.com;

import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import utility.typeSafe.StatusCheckBox;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class HeaderCheckBoxHandler implements TableModelListener{
     private final JTable table;
    private final int targetColumnIndex;
    public HeaderCheckBoxHandler(JTable table, int index) {
        this.table = table;
        this.targetColumnIndex = index;
    }
    @Override public void tableChanged(TableModelEvent e) {
        if(e.getType()==TableModelEvent.UPDATE && e.getColumn()==targetColumnIndex) {
            int vci = table.convertColumnIndexToView(targetColumnIndex);
            TableColumn column = table.getColumnModel().getColumn(vci);
            if(!StatusCheckBox.INDETERMINATE.equals(column.getHeaderValue())) {
                column.setHeaderValue(StatusCheckBox.INDETERMINATE);
            }else{
                boolean selected = true, deselected = true;
                TableModel m = table.getModel();
                for(int i=0; i<m.getRowCount(); i++) {
                    Boolean b = (Boolean)m.getValueAt(i, targetColumnIndex);
                    selected &= b; deselected &= !b;
                    if(selected==deselected) return;
                }
                if(selected) {
                    column.setHeaderValue(StatusCheckBox.SELECTED);
                }else if(deselected) {
                    column.setHeaderValue(StatusCheckBox.DESELECTED);
                }else{
                    return;
                }
            }
            JTableHeader h = table.getTableHeader();
            h.repaint(h.getHeaderRect(vci));
        }
    }
}
